<?php
if(!isset($_REQUEST['key']) || !isset($_REQUEST['cid'])) die('Invalid usage - missing param');
$cid = $_REQUEST['cid'];
$key = $_REQUEST['key'];

define('CID', $cid);
define('READ_ONLY_SESSION',true);
require_once('../../../include.php');
ModuleManager::load_modules();

$path = $_SESSION[$key]['path'];

$start_date = Module::static_get_module_variable($path, 'tong_hop_nhap_xuat_ton_start_date', null);
$start_date = date('Y-m-d', strtotime($start_date));
$end_date = Module::static_get_module_variable($path, 'tong_hop_nhap_xuat_ton_end_date', null);
$end_date = date('Y-m-d', strtotime($end_date));
$employee = Module::static_get_module_variable($path,'tong_hop_nhap_xuat_ton_truong_khu_vuc', null);
$group = Module::static_get_module_variable($path,'tong_hop_nhap_xuat_ton_group', null);
if($group == null){
	$group_is_null = true;
	$group = 0;
}
else{
	$group_is_null = false;
}

if(!empty($employee)){
    $columns = array(
        array('name'=>__('STT'), 'width'=>'8'),
        array('name'=>__('Mã vật tư'), 'width'=>'15'),
        array('name'=>__('Tên vật tư'), 'width'=>'25'),
        array('name'=>__('ĐVT'), 'width'=>'10'),
        array('name'=>__('Tổng nhập trong kỳ'), 'width'=>'15'),
        array('name'=>__('Tổng quyết toán trong kỳ'), 'width'=>'15'),
        array('name'=>__('Tồn cuối kỳ'), 'width'=>'15')
    );

    require_once 'modules/Libs/PHPExcel/lib/PHPExcel.php';
    require_once 'modules/Libs/PHPExcel/lib/PHPExcel/Writer/Excel5.php';

    $objPHPExcel = new PHPExcel();

    $row = 1;$column = 0;
    $sheet = $objPHPExcel->getActiveSheet();

    foreach ($columns as $key=>$value){
        $sheet->getColumnDimensionByColumn($column)->setWidth($value['width']);

        $sheet->setCellValueByColumnAndRow($column, $row, $value['name']);

        $column++;
    }

    $records = DB::GetAll('select * from sctv_tong_hop_nhap_xuat_ton_bao_tri(%D, %D, %d, %b, %d)', array($start_date, $end_date, $employee, $group_is_null, $group));

    $stt = 0;
    foreach($records as $key=>$record){
        $row++;

        $stt++;
        $sheet->setCellValueByColumnAndRow(0, $row, $stt);
        $sheet->setCellValueByColumnAndRow(1, $row, $record['material_code']);
        $sheet->setCellValueByColumnAndRow(2, $row, $record['material_name']);
        $sheet->setCellValueByColumnAndRow(3, $row, $record['material_unit']);
        $input_amount = $record['input_amount'];
        $output_amount = $record['output_amount'];
        $sheet->setCellValueByColumnAndRow(4, $row, $input_amount);
        $sheet->setCellValueByColumnAndRow(5, $row, $output_amount);
        $sheet->setCellValueByColumnAndRow(6, $row, $input_amount - $output_amount);
    }

    $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);

    header('Content-Encoding: UTF-8');
    header('Content-type: text/xls; charset=UTF-8');
    header('Content-disposition: attachement; filename="'. $report .date('Y_m_d__H_i_s').'.xls"');
    $objWriter->save('php://output');
}
?>
